package org.correct.index;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.StringReader;

import java.util.ArrayList;
import java.util.HashMap;
import org.wltea.analyzer.IKSegmentation;
import org.wltea.analyzer.Lexeme;



public class IndexParser extends Thread{
	static boolean lock = false;
	
	static ArrayList<String> stopwords;
	//static HashMap<Integer, Object> termCountMap = new HashMap<Integer, Object>();
	int doc;
	int termNum;
	HashMap<String, Integer> table = new HashMap<String, Integer>();
	
	public IndexParser(int doc){
		this.doc = doc;
		termNum = 0;
	}	
	
	public void parse(String text, int weight){
		try{
			IKSegmentation ikseg = new IKSegmentation(new  StringReader(text), true);
			Lexeme l = ikseg.next();
			while(l != null){
				String s = l.getLexemeText();
				if(inStopwords(s)){
					l = ikseg.next();
					continue;
				}
				termNum++;
				
				if(!table.containsKey(s)){
					table.put(s, weight);
				}else{
					table.put(s, table.get(s) + weight);
				}
				l = ikseg.next();
			}
			
		}catch(Exception e){
			e.printStackTrace();
			System.out.println(doc + text + "索引失败");
		}
	}
	
	public static void getStopWords(){
		File file = new File("dic/stopwords");
		try{
			//System.out.println(file.getAbsolutePath());
			FileInputStream fis = new FileInputStream(file);
			BufferedReader br = new BufferedReader(new InputStreamReader(fis));
			String word = br.readLine();
			stopwords = new ArrayList<String>();
			while(word != null){
				stopwords.add(word);
				word = br.readLine();
			}
			br.close();
		}catch(Exception e){
			e.printStackTrace();
			System.out.println("读stopwords失败");
		}		
	}
	
	boolean inStopwords(String s){
		for(int i = 0; i < stopwords.size(); i++){
			if(s.equals(stopwords.get(i))){
				return true;
			}
			//System.out.println(stopwords.get(i));
		}
		return false;
	}
	
	public void write(){
		try{
			while(lock){
				sleep(50);
			}
			//termCountMap.put(doc, termNum);
			//System.out.println(termNum);
			lock = true;
			new IndexWriter(table, doc, termNum).start();
		}catch (Exception e) {
			e.printStackTrace();
		}
		
	}
}
